package com.qianniu.napi.admin.web;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.qianniu.napi.admin.entity.JsonDO;
import com.qianniu.napi.admin.entity.OrderGoods;
import com.qianniu.napi.admin.service.IOrderGoodsService;
import com.qianniu.napi.common.util.ObjectUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * <p>
 * </p>
 *
 * @author chenxuebin
 * @since 2018-06-28
 */
@RestController
public class OrderGoodsController {

    private static final Logger logger = LoggerFactory.getLogger(OrderGoodsController.class);

    @Autowired
    private IOrderGoodsService ordergoodsService;

    @RequestMapping("/api/ordergoods/list")
    @ResponseBody
    public JsonDO list(@RequestParam(value = "ptype", required = false) String ptype,
                       @RequestParam(value = "oid", required = false) Long oid
    ) {
        JsonDO json = new JsonDO();
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("oid", oid);
        map.put("mpid", ObjectUtil.getMpid());
        map.put("state", 1);

        List<OrderGoods> list = ordergoodsService.selectList(new EntityWrapper<OrderGoods>()
                .eq("oid",oid)
                .eq("mpid",ObjectUtil.getMpid())
                .eq("state",1)
        );
        json.setData(list);
        return json;
    }

    @RequestMapping("/api/ordergoods/add")
    @ResponseBody
    public JsonDO ordergoodsAdd(@Valid OrderGoods ordergoods,
                                BindingResult bindingResult, HttpServletRequest req) {
        JsonDO json = new JsonDO();
        // 字段校验
        if (bindingResult.hasErrors()) {
            logger.debug("There are errors! {}", bindingResult);
            json.setMsg("There are errors! " + bindingResult);
            json.setCode(400);
            return json;
        }
        ordergoods.setCreatetime(new Date());
        ordergoods.insert();
        return json;
    }

    @RequestMapping("/api/ordergoods/modify")
    @ResponseBody
    public JsonDO ordergoodsUpdate(@Valid OrderGoods ordergoods,
                                   BindingResult bindingResult, HttpServletRequest req) {
        JsonDO json = new JsonDO();
        // 字段校验
        if (bindingResult.hasErrors()) {
            logger.debug("There are errors! {}", bindingResult);
            json.setMsg("There are errors! " + bindingResult);
            json.setCode(400);
            return json;
        }
        ordergoods.setModifytime(new Date());
        ordergoods.updateById();
        return json;
    }

    @RequestMapping("/api/ordergoods/detail")
    @ResponseBody
    public JsonDO getOrderGoodsDetailById(@RequestParam("token") String token,
                                          @RequestParam("id") long id, HttpServletRequest req
    ) {
        JsonDO json = new JsonDO();
        OrderGoods ordergoods = ordergoodsService.selectById(id);
        json.setData(ordergoods);
        return json;
    }

}
